/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TStockInOut;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TStockInOutDao;
import com.ruipengkj.service.TStockInOutService;
/**
 * 说明：
 * 入/出库单Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TStockInOutServiceImpl implements TStockInOutService{
	@Resource
	private TStockInOutDao TStockInOutDao;//DAO层
	
	//查询所有
	@Override
	public List<TStockInOut> findService(TStockInOut entity) throws Exception {
		String hql = "select o from TStockInOut o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getSourceType()!= 0){
				whereHQL.append("SourceType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getSourceType()+"%");
			}
			if(entity.getSource() != null && !entity.getSource().equals("")){
				whereHQL.append("Source").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getSource()+"%");
			}
			if(entity.getTStore()!= null&&entity.getTStore().getStoreID()!=null){
				whereHQL.append("o.TStore=?").append(BaseQuery.AND);
				param.add(entity.getTStore());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockInOutDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	//根据条件翻页查询
	@Override
	public PageModel<TStockInOut> getScrollService(int firstResult,
			int maxResult, TStockInOut entity) throws Exception {
		String hql = "select o from TStockInOut o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
			if (entity != null) {
				if(entity.getTStore()!= null&&entity.getTStore().getStoreID()!=null){
					whereHQL.append("o.TStore=?").append(BaseQuery.AND);
					param.add(entity.getTStore());
				}
				if(entity.getSourceType()!= 0){
					whereHQL.append("SourceType=?").append(BaseQuery.AND);
					param.add(entity.getSourceType());
				}
				if(entity.getType()!= 0){
					whereHQL.append("Type=?").append(BaseQuery.AND);
					param.add(entity.getType());
				}
				//按时间查询
				if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
					whereHQL.append("HappenTime>=?").append(BaseQuery.AND);
					param.add(entity.getHappenTime());
				}
				if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
					whereHQL.append("HappenTime<=?").append(BaseQuery.AND);
					param.add(entity.getEndTime());
				}
				if(whereHQL.length()>4)
					whereHQL.delete(whereHQL.length()-5, whereHQL.length());
			}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TStockInOutDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//修改
	@Override
	public void modifyService(TStockInOut entity) throws Exception {
		TStockInOutDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TStockInOut entity) throws Exception {
		TStockInOutDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TStockInOutDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TStockInOut getByIdService(String id) throws Exception {
		return TStockInOutDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TStockInOut entity) throws Exception {
		TStockInOutDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TStockInOut entity) throws Exception {
		return TStockInOutDao.saveRetDao(entity);
	}
	public TStockInOutDao getTStockInOutDao() {
		return TStockInOutDao;
	}

	public void setTStockInOutDao(TStockInOutDao TStockInOutDao) {
		this.TStockInOutDao = TStockInOutDao;
	}

	//根据条件删除
	@Override
	public void deleteService(TStockInOut entity)
			throws Exception {
		String hql = "select o from TStockInOut o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getSourceType()!=0){
				whereHQL.append("SourceType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getSourceType()+"%");
			}
			if(entity.getSource()!=null&&!entity.getSource().equals("")){
				whereHQL.append("Source").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getSource()+"%");
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		List<TStockInOut> list=TStockInOutDao.findDao(hql, whereHQL.toString(), param, null);
		if(list!=null&&list.size()!=0){
				TStockInOutDao.deleteDao(list.get(0).getInOutID());
		}
	}
}